@page "/"
@using Microsoft.Agents.AI
@inject AIAgent Agent
@inject ILogger<Home> Logger
@rendermode @(new InteractiveServerRenderMode(prerender: false))

<PageTitle>Claude Chat - AgentFx</PageTitle>

<div class="chat-container">
    <div class="chat-header">
        <h1>💬 Claude Chat</h1>
        <p class="subtitle">Powered by Microsoft Agent Framework & Microsoft Foundry</p>
    </div>

    <div class="chat-messages" @ref="messagesContainer">
        @foreach (var message in _messages)
        {
            <div class="message @message.Role">
                <div class="message-role">@(message.Role == "user" ? "You" : "Claude")</div>
                <div class="message-content">@message.Content</div>
            </div>
        }
        @if (_isLoading)
        {
            <div class="message assistant">
                <div class="message-role">Claude</div>
                <div class="message-content">
                    <span class="typing-indicator">Thinking...</span>
                </div>
            </div>
        }
    </div>

    <div class="chat-input-container">
        <textarea @bind="_userInput" @bind:event="oninput" @onkeydown="HandleKeyDown"
            placeholder="Type your message here..." rows="3" disabled="@_isLoading" class="chat-input"></textarea>
        <button @onclick="SendMessage" disabled="@(_isLoading || string.IsNullOrWhiteSpace(_userInput))"
            class="btn btn-primary send-button">
            Send
        </button>
    </div>
</div>

@code {
    private ElementReference messagesContainer;
    private List<ChatMessage> _messages = new();
    private string _userInput = string.Empty;
    private bool _isLoading = false;

    private record ChatMessage(string Role, string Content);

    private async Task SendMessage()
    {
        if (string.IsNullOrWhiteSpace(_userInput) || _isLoading)
            return;

        var userMessage = _userInput.Trim();
        _userInput = string.Empty;

        // Add user message
        _messages.Add(new ChatMessage("user", userMessage));
        StateHasChanged();
        await ScrollToBottom();

        // Get AI response
        _isLoading = true;
        StateHasChanged();

        try
        {
            var response = await Agent.RunAsync(userMessage);
            _messages.Add(new ChatMessage("assistant", response.Text));
        }
        catch (Exception ex)
        {
            Logger.LogError(ex, "Error getting AI response");
            _messages.Add(new ChatMessage("assistant", "Sorry, I encountered an error. Please try again."));
        }
        finally
        {
            _isLoading = false;
            StateHasChanged();
            await ScrollToBottom();
        }
    }

    private async Task HandleKeyDown(KeyboardEventArgs e)
    {
        if (e.Key == "Enter" && !e.ShiftKey)
        {
            await SendMessage();
        }
    }

    private async Task ScrollToBottom()
    {
        await Task.Delay(100); // Give UI time to render
                               // Note: In production, use JSInterop to scroll
    }
}